package com.xinweisi.dataserver.module.sysdata.datastatistics;

import com.xinweisi.dataserver.common.domain.PageResultDTO;
import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.memberdata.onlinemember.domain.dto.OnLineMemberQueryDTO;
import com.xinweisi.dataserver.module.memberdata.onlinemember.domain.entity.OnLineMemberVO;
import com.xinweisi.dataserver.module.sysdata.datastatistics.domain.dto.MemberDataQueryDTO;
import com.xinweisi.dataserver.module.sysdata.datastatistics.domain.vo.MemberAvgTimeVO;
import com.xinweisi.dataserver.module.sysdata.datastatistics.domain.vo.MemberDataVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys/sysData")
public class MemberDataController {

    @Autowired
    private MemberDataService memberDataService;


    @ApiOperation(value = "统计数据", notes = "统计数据")
    @RequestMapping(value = "/getMemberData",method= RequestMethod.POST)
    public ResponseDTO<MemberDataVO> getMemberData(@RequestBody @Valid MemberDataQueryDTO queryDTO) throws ParseException {
        log.info("----------统计数据----------");
        List<String> DBNameList = new ArrayList<String>();
        String dateStart=queryDTO.getStartTime();
        String DBName = "";
        String dateEnd=queryDTO.getEndTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Date d1=sdf.parse(dateStart);

        Date d2=sdf.parse(dateEnd);
        long daysBetween=(d2.getTime()-d1.getTime()+1000000)/(60*60*24*1000);
        DBName = "daylog_"+(Long.parseLong(dateStart));
        DBNameList.add(DBName);
        if(daysBetween>0){
            for (int i = 0;i<daysBetween;i++){
                DBName = "daylog_"+(Long.parseLong(dateStart)+1);
                DBNameList.add(DBName);
            }
        }

        queryDTO.setDBNameList(DBNameList);
        return memberDataService.getMemberData(queryDTO);
    }

    @RequestMapping(value = "/getMemberAvgTime",method= RequestMethod.POST)
    public ResponseDTO<MemberAvgTimeVO> getMemberAvgTime() {
        log.info("----------平均时长----------");
        return memberDataService.getMemberAvgTime();
    }

}
